package com.coder.common.util;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.stereotype.Component;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.support.DefaultTransactionDefinition;


@Component
@Scope("prototype")
public class DatabaseUtil {

    @Autowired
    private DataSourceTransactionManager dataSource;

    private TransactionStatus transactionStatus;

    public void begin(){
        transactionStatus = dataSource.getTransaction(new DefaultTransactionDefinition());
    }

    public void commit(TransactionStatus transactionStatus){
        dataSource.commit(transactionStatus);
    }

    public void rollback(TransactionStatus transactionStatus){
        dataSource.rollback(transactionStatus);
    }

    public TransactionStatus getTransactionStatus(){
        return transactionStatus;
    }
}
